package com.ophiux.cs.application.consultBase.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.consultBase.dto.AddHospitalRequest;
import com.ophiux.cs.application.consultBase.dto.AddHospitalResponse;
import com.ophiux.cs.application.consultBase.dto.GetHospitalRequest;
import com.ophiux.cs.application.consultBase.dto.GetHospitalResponse;
import com.ophiux.cs.application.consultBase.dto.hospitalCreateVadilRequest;
import com.ophiux.cs.application.consultBase.service.IAddHospitalAppService;
import com.ophiux.cs.application.consultBase.service.IGetHospitalAppService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.domain.consultBase.model.entity.Hospital;
import com.ophiux.utils.common.ShiftUtils;
import com.ophiux.utils.parser.json.JsonUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 医院综合信息服务
 * @author ADMIN
 *
 */
@Service
@Slf4j
public class HospitalGeneralService {
	@Autowired
	IGetHospitalAppService iGetHospitalAppService;//查询医院信息服务
	@Autowired
	IAddHospitalAppService iAddHospitalAppService;//新增医院信息服务
	
	public Hospital hospitalCreateVadil(hospitalCreateVadilRequest dto) throws ParamException {
		
		dto.verParam();
		
		Hospital hospital = null;
		GetHospitalRequest getHospDto = new GetHospitalRequest();
		getHospDto.setHOSPITAL_NAME(dto.getHOSPITAL_NAME());
		Result<GetHospitalResponse> getHospResult = iGetHospitalAppService.getHospital(getHospDto);
		if(getHospResult != null) {
			if(getHospResult.getRESULT().equals(Result.RESULT_SUCCESS)) {
				if(getHospResult.getRESPONSE() != null && getHospResult.getRESPONSE().getHosList() != null && getHospResult.getRESPONSE().getHosList().size() != 0) {
					GetHospitalResponse getHospRes = getHospResult.getRESPONSE();

					hospital = (Hospital) ShiftUtils.objToNewObj(getHospRes.getHosList().get(0), Hospital.class);
					log.info("【HospitalGeneralService】已经存在该医院信息，暂不处理本种情况。当前医院名称："+dto.getHOSPITAL_NAME());
				}else {
					hospital = new Hospital();
					AddHospitalRequest addHospDto = new AddHospitalRequest();
					addHospDto.setHOSPITAL_NAME(dto.getHOSPITAL_NAME());
					Result<AddHospitalResponse> addHospResult = iAddHospitalAppService.createHospital(addHospDto);
					if(addHospResult != null && addHospResult.getRESULT().equals(Result.RESULT_SUCCESS)) {
						log.info("【HospitalGeneralService】新增医院成功。当前医院名称："+dto.getHOSPITAL_NAME());
						hospital.setHOSPITAL_NAME(addHospDto.getHOSPITAL_NAME());
						hospital.setHOSPITAL_ID(addHospResult.getRESPONSE().getHOSPITAL_ID());
					}else {
						log.info("【HospitalGeneralService】新增医院失败。当前医院名称："+dto.getHOSPITAL_NAME()+"。返回信息为:"+JsonUtils.toJson(addHospResult));
					}
				}
			}else {
				log.info("【HospitalGeneralService】获取医院信息未知错误，暂不处理本种情况。当前医院名称："+dto.getHOSPITAL_NAME()+"。返回信息为:"+JsonUtils.toJson(getHospResult));
			}
			
		}else {
			log.info("【HospitalGeneralService】获取医院信息未得到返回信息，暂不处理本种情况。当前医院名称："+dto.getHOSPITAL_NAME());
		}
		
		return hospital;
	}
}
